<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%@page import="jxl.write.*"%>
<%@page import="jxl.*"%>
<%@page import="java.io.OutputStream"%>
<%@page import="java.util.List"%>
<%@page import="hrms.entity.History"%>
<%@page import="hrms.service.*"%>
<%@page import="java.lang.reflect.Field"%>
<%@page import="org.springframework.context.ApplicationContext"%>
<%@page import="org.springframework.context.support.ClassPathXmlApplicationContext"%>
<html>
	<%
	ApplicationContext ctx = new ClassPathXmlApplicationContext("conf/spring-*.xml");
	HistoryServiceImpl hsi = ctx.getBean("historyService",HistoryServiceImpl.class);
	List<History> list = hsi.Excel();
	String result = "系统提示：Excel文件导出成功";
	String[] Title={"序号","用户名","员工号","员工名","角色","登录时间","退出时间"};
	String fileName="登录日志.xls";
	
//	Test t=new Test();
//	t.setAa("11");
//	listContent.add(t);
	
	// 以下输出到EXCEL
	try {
		// 定义输出流，以便打开保存对话框______________________begin
		// HttpServletResponse response=ServletActionContext.getResponse();
		OutputStream os = response.getOutputStream();// 取得输出�?
		response.reset();// 清空输出�?
		response.setHeader("Content-disposition",
				"attachment; filename=" + new String(fileName.getBytes("GB2312"), "ISO8859-1"));
		// 设定输出文件�?
		response.setContentType("application/msexcel");// 定义输出类型
		//response.setContentType("application/msexcel;charset=ISO8859-1");
		// 定义输出流，以便打开保存对话框_______________________end

		/** **********创建工作�?*********** */
		WritableWorkbook workbook = Workbook.createWorkbook(os);

		/** **********创建工作�?*********** */

		WritableSheet sheet = workbook.createSheet("Sheet1", 0);

		/** **********设置纵横打印（默认为纵打）�?打印�?**************** */
		jxl.SheetSettings sheetset = sheet.getSettings();
		sheetset.setProtected(false);

		/** ************设置单元格字�?************* */
		WritableFont NormalFont = new WritableFont(WritableFont.ARIAL, 10);
		WritableFont BoldFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD);

		/** ************以下设置三种单元格样式，灵活备用************ */
		// 用于标题居中
		WritableCellFormat wcf_center = new WritableCellFormat(BoldFont);
		wcf_center.setWrap(true);
		wcf_center.setBorder(Border.ALL, BorderLineStyle.THIN); // 线条
		wcf_center.setVerticalAlignment(VerticalAlignment.CENTRE); // 文字垂直对齐
		wcf_center.setAlignment(Alignment.CENTRE); // 文字水平对齐
		wcf_center.setWrap(false); // 文字是否换行
		
		// 用于正文居左
		WritableCellFormat wcf_left = new WritableCellFormat(NormalFont);
		wcf_left.setWrap(true);
		wcf_left.setBorder(Border.NONE, BorderLineStyle.THIN); // 线条
		wcf_left.setVerticalAlignment(VerticalAlignment.CENTRE); // 文字垂直对齐
		wcf_left.setAlignment(Alignment.LEFT); // 文字水平对齐
		wcf_left.setWrap(false); // 文字是否换行

		/** ***************以下是EXCEL�?��大标题，暂时省略********************* */
		// sheet.mergeCells(0, 0, colWidth, 0);
		// sheet.addCell(new Label(0, 0, "XX报表", wcf_center));
		/** ***************以下是EXCEL第一行列标题********************* */
		sheet.setColumnView(7,30);
		sheet.setColumnView(8,30);
		for (int i = 0; i < Title.length; i++) {
			sheet.addCell(new Label(i, 0, Title[i], wcf_center));
		}
		/** ***************以下是EXCEL正文数据********************* */
		Field[] fields = null;
		int i = 1;
		for (Object obj : list) {
			fields = obj.getClass().getDeclaredFields();
			int j = 0;
			for (Field v : fields) {
				v.setAccessible(true);
				Object va = v.get(obj);
				if (va == null) {
					va = "";
				}
				if(va.equals(0)){
					va = "总裁";
				}
				if(va.equals(1)){
					va = "经理";
				}
				if(va.equals(2)){
					va = "员工";
				}
				sheet.addCell(new Label(j, i, va.toString(), wcf_left));
				j++;
			}
			i++;
		}
		/** **********将以上缓存中的内容写到EXCEL文件�?******* */
		workbook.write();
		/** *********关闭文件************* */
		workbook.close();
	} catch (Exception e) {
		result = "系统提示：Excel文件导出失败，原因：" + e.toString();
		System.out.println(result);
		e.printStackTrace();
	}

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	%>
</html>